package cloud.daodao.storm.common.helper;

import cloud.daodao.storm.common.config.CommonConfig;
import cloud.daodao.storm.common.util.security.AesUtil;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SecurityHelper {

    @Resource
    private CommonConfig commonConfig;

    public String aesEncrypt(String plains) {
        String aesKey = commonConfig.getAesKey();
        String aesIv = commonConfig.getAesIv();
        String cipher = null;
        try {
            cipher = AesUtil.encrypt(aesKey, aesIv, plains);
        } catch (Exception e) {
            log.error("AES encrypt error", e);
            // throw new RuntimeException(e);
        }
        return cipher;
    }

    public String aesDecrypt(String cipher) {
        String aesKey = commonConfig.getAesKey();
        String aesIv = commonConfig.getAesIv();
        String plains = null;
        try {
            plains = AesUtil.decrypt(aesKey, aesIv, cipher);
        } catch (Exception e) {
            log.error("AES decrypt error", e);
            // throw new RuntimeException(e);
        }
        return plains;
    }

}
